import time
import asyncio

"""
time.sleep() 是阻塞的，协程执行到此会导致整体事件循环卡住
asyncio.sleep() 非阻塞，事件循环将运行其他逻辑
"""

# BAD
async def execute_task(task_id: int):
    print(f"task[{task_id}] hello")
    time.sleep(1)
    print(f"task[{task_id}] world")


# GOOD
async def execute_task2(task_id: int):
    print(f"task[{task_id}] hello")
    await asyncio.sleep(1)
    print(f"task[{task_id}] world")


import asyncio


async def main():
    await asyncio.gather(execute_task(1), execute_task(2))

async def main2():
    await asyncio.gather(execute_task2(1), execute_task2(2))


asyncio.run(main2())
